import fileio from '@ohos.fileio';
import featureAbility from '@ohos.ability.featureAbility';
export default {
    data: {
        title: ""
    },
    onInit() {
        this.title = "Hello World";
    },
    record() {
        console.log("gyf record");
        globalThis.isRecord = true;
        var context = featureAbility.getContext();


        context.getFilesDir().then(function (dir) {
            globalThis.path = dir + "/audio_record.wav";
            console.log("gyf path = " + globalThis.path);
            //打开录音文件
            globalThis.fd = fileio.openSync(globalThis.path, 0o2|0o100|0o1000, 0o666);


            //wav文件写上头部数据 start
            let header = globalThis.encodeWAV();
            console.log("gyf header len = " + header.buffer.byteLength);
            fileio.writeSync(globalThis.fd, header.buffer);
            //wav文件写上头部数据 end


            globalThis.capturer.start().then(function () {
                console.log("gyf start");
                globalThis.capturer.getBufferSize((err, bufferSize) => {
                    if (err) {
                        console.error('gyf getBufferSize error');
                    } else {
                        console.log("gyf bufferSize = " + bufferSize);
                        globalThis.getBuf(bufferSize);
                    }
                });

            });
        });

    },
    recordstop() {
        globalThis.isRecord = false;
        console.log("gyf recordstop");
        globalThis.capturer.stop().then(function (result) {
            console.log("gyf stop result = " + result);
            //            globalThis.capturer.release((err) => {
            //                if (err) {
            //                    console.error('gyf capturer release failed');
            //                } else {
            //                    console.log('gyf capturer released.');
            //                }
            //            });
        });
    },
}
